package mylist;
import  java.util.Arrays;
//四
public class MyArrayList implements IList {
    public int[] elem ;
    public int usedSize;//0
    //顺序表的 默认大小
    public static final int DEFAULT_SIZE = 10;

    public MyArrayList() {
        this.elem = new int[DEFAULT_SIZE];
    }

    public MyArrayList(int capacity) {
        this.elem = new int[capacity];
    }

    /**
     * 遍历顺序表当中的元素
     */
    @Override
    public void display() {
        for (int i = 0; i < this.usedSize; i++) {
            System.out.print(this.elem[i]+" ");
        }
        System.out.println();
    }


    @Override
    public void add(int data) {
        checkCapacity();

        this.elem[this.usedSize] = data;
        this.usedSize++;
    }

    @Override
    public boolean isFull() {
        /*if(usedSize == elem.length) {
            return true;
        }
        return false;*/

        return usedSize == elem.length;
    }

    @Override
    public void add(int pos, int data) {
        try {
            checkPosOnAdd(pos);
        }catch (PosIllegality e) {
            e.printStackTrace();
            return;
        }
        checkCapacity();
        //1、从最后一个有效的数据开始往后移动 //2、当i < pos 就结束
        for (int i = usedSize-1; i >= pos; i--) {
            elem[i+1] = elem[i];
        }
        //3、存放元素到pos 位置
        elem[pos] = data;
        //4、usedSize++;
        usedSize++;
    }
    /**
     * 检查pos的合法性
     */
    private void checkPosOnAdd(int pos) throws PosIllegality{
        if(pos < 0 || pos > usedSize) {
            System.out.println("不符合法！");
            throw new PosIllegality("插入元素下标异常: "+pos);
        }
    }

    private void checkCapacity() {
        if(isFull()) {
            //扩容
            elem = Arrays.copyOf(elem,elem.length*2);
        }
    }

    @Override
    public boolean contains(int toFind) {
        if(isEmpty()) {
            return false;
        }
        for (int i = 0; i < usedSize; i++) {
            //如果是查找引用数据类型 一定记住 重写方法
            if(elem[i] == toFind) {
                return true;
            }
        }
        return false;
    }

    public boolean isEmpty() {
        return usedSize == 0;
    }

    @Override
    public int indexOf(int toFind) {
        if(isEmpty()) {
            return -1;
        }
        for (int i = 0; i < usedSize; i++) {
            //如果是查找引用数据类型 一定记住 重写方法
            if(elem[i] == toFind) {
                return i;
            }
        }
        return -1;
    }

    @Override
    public int get(int pos) throws MyArrayListEmpty{
        checkPosOnGetAndSet(pos);

        if(isEmpty()) {
            throw new MyArrayListEmpty("获取指定下标元素时" +
                    "顺序表为空！");
        }

        return elem[pos];
    }


    private void checkPosOnGetAndSet(int pos) throws PosIllegality{
        if(pos < 0 || pos >= usedSize) {
            System.out.println("不符合法！");
            throw new PosIllegality("获取指定下标的元素异常: "+pos);
        }
    }

    @Override
    public void set(int pos, int value) {
        checkPosOnGetAndSet(pos);

        elem[pos] = value;
    }

    @Override
    public void remove(int toRemove) {
        int index = indexOf(toRemove);
        if(index == -1) {
            System.out.println("没有这个数字！");
            return;
        }
        for(int i = index; i < usedSize-1;i++) {
            elem[i] = elem[i+1];
        }
        usedSize--;

    }

    @Override
    public int size() {
        return this.usedSize;
    }

    @Override
    public void clear() {
        this.usedSize = 0;
    }
}